﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using WindowsGame1.ItemClasses;

namespace WindowsGame1
{
    public enum CharClass { Pirate = 0, BountyHunter = 1, SlaveTrader = 2 };

    abstract class PlayerCharacter : Microsoft.Xna.Framework.DrawableGameComponent
    {
        
        static string[] classNames = Enum.GetNames(typeof(CharClass));

        protected string name;
        protected bool gender;

        protected int[] hitPoints = new int[2];
        protected int[] spellPoints = new int[2];

        protected Level difficulty;
        protected int level;
        protected ulong gold;
        protected ulong experience;

        protected CharacterAbilities abilities = new CharacterAbilities();

        protected string className;
        protected Game1 game;

        protected SpriteBatch spriteBatch;
        protected ContentManager Content;

        protected Texture2D hpspDisplay;
        protected Texture2D blueBar;
        protected Texture2D redBar;

        protected SpriteFont spriteFont;
        protected Vector2 hpspPosition;


        protected List<BaseItem> backpack = new List<BaseItem>();
        protected Weapon weapon;
        protected Armor armor;
        protected Shield shield;


        public PlayerCharacter(Game1 game)
            : base(game)
        {
            this.game = game;
            spriteBatch =(SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            Content =(ContentManager)Game.Services.GetService(typeof(ContentManager));

            difficulty = Level.Normal;
            level = 1;
            experience = 0;
            gold = 0;

            hpspDisplay = Content.Load<Texture2D>(@"GUI\hpspborder");
            blueBar = Content.Load<Texture2D>(@"GUI\bluebar");
            redBar = Content.Load<Texture2D>(@"GUI\redbar");
            spriteFont = Content.Load<SpriteFont>(@"smallFont");
            hpspPosition = new Vector2(10, 10);
        }

        public string Name
        {
            get { return name; }
        }

        public string ClassName
        {
            get { return className; }
        }

        public static string[] ClassNames
        {
            get { return classNames; }
        }

        public int HitPointsMax
        {
            get { return hitPoints[1]; }
        }

        public int HitPointsCurrent
        {
            get { return hitPoints[0]; }
            set { hitPoints[0] = value; }
        }

        public int SpellPointsMax
        {
            get { return spellPoints[1]; }
        }

        public int SpellPointsCurrent
        {
            get { return spellPoints[0]; }
        }

        public int PlayerLevel
        {
            get { return level; }
        }

        public ulong Gold
        {
            get { return gold; }
        }

        public void AddGold(int gold)
        {
            this.gold += (ulong)gold;
        }

        public ulong Experience
        {
            get { return experience; }
        }

        public void AddExperience(int exp)
        {
            this.experience += (ulong)exp;
        }

        public Level DifficultyLevel
        {
            get { return difficulty; }
        }

        public virtual CharacterAbilities Abilities
        {
            get { return abilities; }
        }

        public virtual Weapon Weapon
        {
            get { return weapon; }
            set { weapon = value; }
        }

        public virtual Armor Armor
        {
            get { return armor; }
            set { armor = value; }
        }

        public virtual Shield Shield
        {
            get { return shield; }
            set { shield = value; }
        }

        public void Show()
        {
            Visible = true;
        }

        public void Hide()
        {
            Visible = false;
        }

        public override void Draw(GameTime gameTime)
        {
            DrawHitPointsSpellPoints();
            base.Draw(gameTime);
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        private void DrawHitPointsSpellPoints()
        {
            Vector2 barPosition = new Vector2();
            Vector2 textPosition = new Vector2();
            Vector2 stringSize;
            string text;
            Color tintColor = Color.White;
            tintColor.A = 128;
            spriteBatch.Draw(hpspDisplay, hpspPosition, tintColor);
            barPosition = hpspPosition + Vector2.One * 5;
            spriteBatch.Draw(redBar, barPosition, tintColor);
            textPosition = barPosition;
            textPosition.Y -= 5;
            text = hitPoints[0].ToString() + "/" + hitPoints[1].ToString();
            stringSize = spriteFont.MeasureString(text);
            textPosition.X += (200 - stringSize.X) / 2;
            textPosition.Y += (20 - spriteFont.LineSpacing) / 2;
            spriteBatch.DrawString(spriteFont, text, textPosition, tintColor);
            barPosition.Y += 19;
            spriteBatch.Draw(blueBar, barPosition, tintColor);
            textPosition = barPosition;
            textPosition.Y -= 5;
            text = spellPoints[0].ToString() + "/" + spellPoints[1].ToString();
            stringSize = spriteFont.MeasureString(text);
            textPosition.X += (200 - stringSize.X) / 2;
            textPosition.Y += (20 - spriteFont.LineSpacing) / 2;
            spriteBatch.DrawString(spriteFont, text, textPosition, tintColor);
        }
    }
}